package controllers

import (
	"animals/cache"
	"animals/models"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"time"
)

type AnControllers struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *AnControllers) Add() {
	name := c.GetString("name")
	address := c.GetString("address")
	img := c.GetString("img")
	//if name == "" || address == "" || img == "" {
	//	c.Data["json"] = Res{
	//		Code: 0,
	//		Msg:  "参数为空",
	//		Data: nil,
	//	}
	//	c.ServeJSON()
	//}
	animals := models.Animals{
		Name:    name,
		Address: address,
		Img:     img,
	}
	err := models.DB.Create(&animals).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *AnControllers) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件获取失败",
			Data: nil,
		}
		c.ServeJSON()
	}
	if header.Size > 2*1024*1024 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件不能超过2mb",
			Data: nil,
		}
		c.ServeJSON()
	}
	defer file.Close()
	path := "static/img/" + header.Filename
	c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "上传成功",
			Data: "127.0.0.1:8080" + path,
		}
		c.ServeJSON()
	}
}
func (c *AnControllers) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Where("id=?", id).Delete(&models.Animals{}, id).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *AnControllers) Show() {
	//id, _ := c.GetInt("id")
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var an []models.Animals
	key := "an_show"
	res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	//err := models.DB.Where("id=?", id).First(&an, id).Error
	err := models.DB.Find(&an).Error
	if res == "" {
		if err != nil {
			jsonData, _ := json.Marshal(&an)
			cache.Rdb.Set(cache.Ctx, key, jsonData, time.Minute*2)
			c.Data["json"] = Res{
				Code: 200,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()

		} else {
			json.Unmarshal([]byte(res), &an)
			c.Data["json"] = Res{
				Code: 200,
				Msg:  "查询成功",
				Data: an,
			}
			c.ServeJSON()
		}
	}

}
func (c *AnControllers) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var an []models.Animals

	name := c.GetString("name")
	query := models.DB.Model(&models.Animals{})
	if name == "" {
		query = query.Where("name like ?", "%"+"%")
	}
	key := "an_show"
	res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	var count int64
	query.Count(&count)
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	offset := (page - 1) * size
	err := query.Limit(size).Offset(offset).Find(size).Error
	if err != nil {
		jsonData, _ := json.Marshal(&an)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Minute*2)
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()

	} else {
		json.Unmarshal([]byte(res), &an)
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: an,
		}
		c.ServeJSON()
	}

}
